require 'openssl'
require 'digest'
require 'jsonify'
module WorkManageService
  AppId = "37020320190310001"
  SecretKey = "2622c126e4829d2c121de0b26e77aa31"
  URL = "http://jzgr.mohurd.gov.cn:81/open.api"
  Version = "1.0"
  Format = "json"
  def self.test()
    conn = Faraday.new(:url => URL)

    timestamp = WorkManageService.get_timestamp()
    format = "json"
    version = "1.0"
    app_id = AppId
    nonce = rand(999999999).to_s
    app_secret = SecretKey
    method = "Corp.Query"
    data = {'corpCode':'91370203MA3C7NDA94','pageIndex':1,'pageSize':10}.to_json
    sign = WorkManageService.get_sign(app_id, data, format, method, timestamp, nonce, version, app_secret)
    req_body = "method="+mothod+"&format="+format+"&version="+version+"&appid="+app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
  end
  def self.project_team(team_id, project_id)
    conn = Faraday.new(:url => URL)
    method = "Team.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    team = ProjectTeam.find(team_id)
    project = Project.find(project_id)
    team_data = Jsonify::Builder.new
    team_data.projectCode project.app_id
    team_data.teamName team.team_name
    team_data.corpName project.construction_name
    team_data.corpCode project.construction_code
    data = team_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
    respons = conn.post do |req|
      req.body = req_body.to_s
    end

    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      yibu = YiBu.new
      yibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      yibu.m_type = 2  ## 班组上传
      yibu.m_id = team.id
      yibu.save
      return 0
    else
      return 400
    end
  end
  def self.worker_upload(user_list, project_id)
    conn = Faraday.new(:url => URL)
    method = "ProjectWorker.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    user_list = user_list.split(";")
    project = Project.find(project_id)
    user_data = Jsonify::Builder.new
    user_data.projectCode project.app_id
    user_data.corpName project.construction_name
    user_data.corpCode project.construction_code
    user_data.teamSysNo  "1500151787"
    user_data.teamName "项目组"
    user_data.workerList(user_list)  do |ul|
      user = User.find(ul)
      user_detail = UserDetail.where("user_id = ?", user.id).first
      user_data.workerName user.name
      user_data.isTeamLeader 0
      user_data.idCardType "01" 
      user_data.idCardNumber CommonApi.jiami_aes(project.secret_key,user.identity_card)
      # user_data.idCardNumber "KuSQPglje/JSiPt+FY7UC9xssOZ07OLvqUOx8C6qv0Y="
      user_data.workType "010"
      user_data.workRole 20
      user_data.nation "汉"
      user_data.address "山东省青岛市"
      user_data.headImage user_detail.head_img_url
      user_data.politicsType "13"
      user_data.cellPhone user.phone
      user_data.cultureLevelType "03"
      user_data.grantOrg user_detail.licensing_organizations
    end
    data = user_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      yibu = YiBu.new
      yibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      yibu.m_type = 2  ## 班组上传
      yibu.m_id = project.id
      yibu.save
      return 0
    else
      return 400
    end
  end
  def self.gz_upload(gz_yb_id)
    conn = Faraday.new(:url => URL)
    method = "Payroll.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    gzyb = GZYiBu.find(gz_yb_id)
    payroll = PayrollDetail.where(id: gzyb.gz_id)
    project_user = ProjectUser.find(payroll.first.project_user_id)
    project = Project.find(project_user.project_id)
    user = User.find(project_user.user_id)
    user_detail = UserDetail.where(user_id: user.id).first
    project_team = ProjectTeam.find(project_user.upload_project_team)
    company = Company.find(project_team.company_id)
    if user_detail.pay_card.nil?
      qr_log = QrLog.new
      qr_log.project_id = kqyb.project_id
      qr_log.MSG = "您发起的#{user.name}员工工资上传失败，错误原因为：员工没有维护银行卡信息"
      qr_log.save
    end
    project_team = ProjectTeam.find(project_user.upload_project_team)
    gz_data = Jsonify::Builder.new
    gz_data.projectCode project.app_id
    gz_data.corpName company.company_name
    gz_data.corpCode company.credit_code
    gz_data.teamSysNo  project_team.team_code
    gz_data.payMonth "2019-03"
    gz_data.detailList(payroll)  do |pl|
      user_detail = UserDetail.where("user_id = ?", user.id).first
      gz_data.idCardType "01" 
      gz_data.idCardNumber CommonApi.jiami_aes(project.secret_key,user.identity_card)
      gz_data.days pl.work_days
      gz_data.workHours pl.work_hours
      gz_data.payRollBankCardNumber CommonApi.jiami_aes(project.secret_key,user_detail.pay_card)
      gz_data.payRollBankCode project.bank_code.to_s
      gz_data.payRollBankName project.bank_name
      gz_data.payBankCardNumber CommonApi.jiami_aes(project.secret_key,project.bank_card)
      gz_data.payBankCode project.bank_code.to_s
      gz_data.payBankName project.bank_name
      gz_data.totalPayAmount pl.totalPayAmount
      gz_data.actualAmount pl.actualAmount
      gz_data.isBackPay 0
      gz_data.balanceDate pl.updated_at.strftime("%Y-%m-%d")
      gz_data.thirdPayRollCode pl.paroll_code
    end
    data = gz_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    #### 补充 上传错误状态 ###
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"     
      gzyb.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      gzyb.upload_status = 1
      gzyb.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = gzyb.project_id
      qr_log.MSG = "您发起的员工工资上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      gzyb.upload_status = 3
      gzyb.save
    end
  end

  def self.kq_upload(kq_yb_id)
    conn = Faraday.new(:url => URL)
    method = "WorkerAttendance.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    kqyb = KQYiBu.find(kq_yb_id)
    sign = Sign.where(id: kqyb.kq_id)
    project = Project.find(sign.first.project_id)
    project_user = ProjectUser.where(user_id: sign.first.user_id, project_id: project.id).first
    project_team = ProjectTeam.find(project_user.upload_project_team)
    user = User.find(project_user.user_id)
    kq_data = Jsonify::Builder.new
    kq_data.projectCode project.app_id
    kq_data.teamSysNo  project_team.team_code
    kq_data.dataList(sign)  do |pl|
      user = User.find(project_user.user_id)
      user_detail = UserDetail.where("user_id = ?", user.id).first
      kq_data.idCardType "01" 
      kq_data.idCardNumber CommonApi.jiami_aes(project.secret_key,user.identity_card)
      kq_data.date pl.create_at.strftime("%Y-%m-%d %H:%M:%S")
      kq_data.direction "01"
    end
    kq_data.dataList(sign)  do |pl|
      user = User.find(project_user.user_id)
      user_detail = UserDetail.where("user_id = ?", user.id).first
      kq_data.idCardType "01" 
      kq_data.idCardNumber CommonApi.jiami_aes(project.secret_key,user.identity_card)
      kq_data.date pl.update_at.strftime("%Y-%m-%d %H:%M:%S")
      kq_data.direction "02"
    end
    data = kq_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    #### 补充 上传错误状态 ###
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"     
      kqyb.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      kqyb.upload_status = 1
      kqyb.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = kqyb.project_id
      qr_log.MSG = "您发起的#{user.name}员工考勤上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      kqyb.upload_status = 3
      kqyb.save
    end
  end

  def self.ry_upload(ry_yb_id)
    conn = Faraday.new(:url => URL)
    method = "ProjectWorker.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    ryyibu = RYYiBu.find(ry_yb_id)
    project_user = ProjectUser.find(ryyibu.ry_id)
    user_list = User.where("id = ?",project_user.user_id)
    project = Project.find(ryyibu.project_id)
    project_team = ProjectTeam.find(project_user.upload_project_team)
    company = Company.find(project_team.company_id)
    user = User.find(project_user.user_id)
    user_data = Jsonify::Builder.new
    user_data.projectCode project.app_id
    user_data.corpName company.company_name
    user_data.corpCode company.credit_code
    user_data.teamSysNo project_team.team_code
    user_data.teamName project_team.team_name
    user_data.workerList(user_list)  do |ul|
      user_detail = UserDetail.where("user_id = ?", ul.id).first
      user_data.workerName ul.name
      user_data.isTeamLeader 0
      user_data.idCardType "01" 
      user_data.idCardNumber CommonApi.jiami_aes(project.secret_key,ul.identity_card)
      # user_data.idCardNumber "KuSQPglje/JSiPt+FY7UC9xssOZ07OLvqUOx8C6qv0Y="
      # "9P+7176Xxfusg\/aRpYvKW\/bzmJS3am+G6J5Qe99JjyA="#
      user_data.workType  CommonApi.cover_job_to_string(user_detail.job_id)
      user_data.workRole user_detail.worker_type.to_i
      ####待优化的地方#####
      user_data.nation "汉"
      user_data.address user_detail.address
      user_data.headImage user_detail.head_img_url
      user_data.politicsType user_detail.politics_type.to_s
      user_data.cellPhone ul.phone
      user_data.cultureLevelType "03"
      user_data.grantOrg user_detail.licensing_organizations
    end
    data = user_data.compile!

    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body =req_body
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      ryyibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      ryyibu.upload_status = 1
      ryyibu.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = ryyibu.project_id
      qr_log.MSG = "您发起的#{user.name}员工信息上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      ryyibu.upload_status = 3
      ryyibu.save
    end
  end
  def self.bz_upload(bz_yb_id)
    conn = Faraday.new(:url => URL)
    method = "Team.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    bzyibu = BZYiBu.find(bz_yb_id)
    project_team  = ProjectTeam.find(bzyibu.bz_id)
    project = Project.find(project_team.project_id)
    company = Company.find(project_team.company_id)
    bz_data = Jsonify::Builder.new
    bz_data.projectCode project.app_id
    bz_data.corpName company.company_name
    bz_data.corpCode company.credit_code
    bz_data.teamName project_team.team_name
   
    data = bz_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      bzyibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      bzyibu.upload_status = 1
      bzyibu.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = bzyibu.project_id
      qr_log.MSG = "您发起的#{company.company_name}班组上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      bzyibu.upload_status = 3
      bzyibu.save
    end
  end
  def self.cj_upload(cj_yb_id)
    conn = Faraday.new(:url => URL)
    method = "ProjectSubContractor.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    cjyibu = CJYiBu.find(cj_yb_id)
    company = Company.find(cjyibu.cj_id)
    project = Project.find(cjyibu.project_id)
    if project.certification_company == company.id
      corpType = "009"
    else
      corpType = "008"
    end
    cj_data = Jsonify::Builder.new
    cj_data.projectCode project.app_id
    cj_data.corpName company.company_name
    cj_data.corpCode company.credit_code
    cj_data.corpType corpType
    data = cj_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
    
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      cjyibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      cjyibu.upload_status = 1
      cjyibu.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = cjyibu.project_id
      qr_log.MSG = "您发起的#{company.company_name}参见单位上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      cjyibu.upload_status = 3
      cjyibu.save
    end
  end
  def self.qy_upload(qy_yb_id)
    conn = Faraday.new(:url => URL)
    method = "Corp.Upload"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    qyyibu = QYYiBu.find(qy_yb_id)
    company = Company.find(qyyibu.qy_id)
    project = Project.find(qyyibu.project_id)
    qy_data = Jsonify::Builder.new
    qy_data.projectCode project.app_id
    qy_data.corpName company.company_name
    qy_data.corpCode company.credit_code
    qy_data.areaCode company.register_address.to_s
    qy_data.corpType company.register_type
    data = qy_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      qyyibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      qyyibu.upload_status = 1
      qyyibu.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = qyyibu.project_id
      qr_log.MSG = "您发起的#{company.company_name}企业上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      qyyibu.upload_status = 3
      qyyibu.save
    end
  end


  def self.jtc_upload(jtc_id)
    conn = Faraday.new(:url => URL)
    method = "WorkerEntryExit.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s

    jtc_yibu = JTCYiBu.find(jtc_id)
    project = Project.find(jtc_yibu.project_id)
    project_user = ProjectUser.where(id: jtc_yibu.jtc_id)
    project_team = ProjectTeam.find(project_user.first.upload_project_team)
    company = Company.find(project_team.company_id)
    jtc_data = Jsonify::Builder.new
    jtc_data.projectCode project.app_id
    jtc_data.corpName company.company_name
    jtc_data.corpCode company.credit_code
    jtc_data.teamSysNo  project_team.team_code
    jtc_data.workerList(project_user)  do |wl|
      user =User.find(wl.user_id)
      user_detail = UserDetail.where("user_id = ?", wl.user_id).first
      jtc_data.idCardType "01" 
      jtc_data.idCardNumber CommonApi.jiami_aes(project.secret_key,user.identity_card)
      jtc_data.date wl.join_time.strftime("%Y-%m-%d")
      jtc_data.type 1
    end
    data = jtc_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      jtc_yibu.requestSerialCode = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      jtc_yibu.upload_status = 1
      jtc_yibu.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = qyyibu.project_id
      qr_log.MSG = "您发起的#{user.name}员工进退场上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
      jtc_yibu.upload_status = 3
      jtc_yibu.save
    end
  end

  def self.ht_upload(pu_id)
    conn = Faraday.new(:url => URL)
    method = "WorkerContract.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    project_user = ProjectUser.where(id: pu_id)
    if project_user.first.upload_project_team.nil?
    else
      project = Project.find(project_user.first.project_id)
      project_team = ProjectTeam.find(project_user.first.upload_project_team)
      company = Company.find(project_team.company_id)
      ht_data = Jsonify::Builder.new
      ht_data.projectCode project.app_id
      ht_data.contractList(project_user)  do |wl|
        user =User.find(wl.user_id)
        user_detail = UserDetail.where("user_id = ?", wl.user_id).first
        ht_data.idCardType "01" 
        ht_data.corpName company.company_name
        ht_data.corpCode company.credit_code
        ht_data.idCardNumber CommonApi.jiami_aes(project.secret_key,user.identity_card)
        ht_data.contractPeriodType wl.contract_type
        ht_data.startDate wl.contract_begin_time
        ht_data.endDate wl.contract_delete_time
      end
      data = ht_data.compile!
      sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
      req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
      respons = conn.post do |req|
        req.body = req_body.to_s
      end
      if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
        project_user.first.contract_yibu = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
        project_user.first.contract_upload_status = 1
        project_user.first.save
        return 0
      else
        qr_log = QrLog.new
        qr_log.project_id = qyyibu.project_id
        qr_log.MSG = "您发起的#{user.name}员工合同上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
        qr_log.save
        project_user.first.contract_upload_status = 3
        project_user.first.save
      end
    end
    
    
  end


  def self.px_upload(pu_id)
    conn = Faraday.new(:url => URL)
    method = "ProjectTraining.Add"
    timestamp = WorkManageService.get_timestamp()
    nonce = rand(999999999).to_s
    project_user = ProjectUser.where(id: pu_id).first
    project = Project.find(project_user.project_id)
    project_team = ProjectTeam.find(project_user.upload_project_team)
    company = Company.find(project_team.company_id)
    user = User.find(project_user.user_id)
    px_data = Jsonify::Builder.new
    px_data.projectCode project.app_id
    px_data.trainingDate project_user.train_time.strftime("%Y-%m-%d")
    px_data.trainingDuration 1
    px_data.trainingName project_user.train_name
    px_data.trainingTypeCode project_user.train_type.to_s
    data = px_data.compile!
    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)

    req_body ="method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
    respons = conn.post do |req|
      req.body = req_body.to_s
    end
    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
      project_user.train_yibu = JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["requestSerialCode"]
      project_user.train_upload_status = 1
      project_user.save
      return 0
    else
      qr_log = QrLog.new
      qr_log.project_id = project.id
      qr_log.MSG = "您发起的#{user.name}员工培训上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
      qr_log.save
       project_user.train_upload_status = 3
      project_user.save
    end
  end

  ####通用方法#####
  def self.get_timestamp()
    time = Time.new
    time_year = time.year.to_s
    time_month = time.month.to_s
    time_day = time.day.to_s
    time_hour = time.hour.to_s
    time_min = time.min.to_s
    time_sec = time.sec.to_s
    if time_year.length != 4
      time_year = "0" + time_year
    end
    if time_month.length != 2
      time_month = "0" + time_month
    end
    if time_day.length != 2
      time_day = "0" + time_day
    end
    if time_hour.length != 2
      time_hour = "0" + time_hour
    end
    if time_min.length != 2
      time_min = "0" + time_min
    end
    if time_sec.length != 2
      time_sec = "0" + time_sec
    end
    return timestamp = time_year + time_month + time_day + time_hour + time_min + time_sec
  end
  def self.get_timestamp_other(time)
    time = time
    time_year = time.year.to_s
    time_month = time.month.to_s
    time_day = time.day.to_s
    time_hour = time.hour.to_s
    time_min = time.min.to_s
    time_sec = time.sec.to_s
    if time_year.length != 4
      time_year = "0" + time_year
    end
    if time_month.length != 2
      time_month = "0" + time_month
    end
    if time_day.length != 2
      time_day = "0" + time_day
    end
    if time_hour.length != 2
      time_hour = "0" + time_hour
    end
    if time_min.length != 2
      time_min = "0" + time_min
    end
    if time_sec.length != 2
      time_sec = "0" + time_sec
    end
    return timestamp = time_year + "-"+ time_month + "-"+  time_day
  end
  def self.get_sign(app_id, data, method, timestamp, nonce, appsecret)
    sign_yuanshi = ("appid="+app_id+"&data="+data+"&format="+WorkManageService::Format+"&method="+method+"&nonce="+nonce+"&timestamp="+timestamp+"&version="+WorkManageService::Version+"&appsecret="+appsecret).to_s
    sign = Digest::SHA256.hexdigest(sign_yuanshi.downcase)
    return sign
  end
end